package com.briup.mt.web.controller;

import com.briup.mt.bean.BaseFile;
import com.briup.mt.bean.BaseFileExample;
import com.briup.mt.service.IBaseFileService;
import com.briup.mt.utils.ExcelUtils;
import com.briup.mt.utils.Message;
import com.briup.mt.utils.MessageUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Validated
@RequestMapping("/baseFile")
public class BaseFileServiceController {

    @Autowired
    private IBaseFileService baseFileService;

    @ApiOperation(value = "导入到Excel中",notes = "注意！测试的时候请将地址粘贴到浏览器地址栏测试", produces ="baseFile/octet-stream")
    @GetMapping("download")
    @ApiImplicitParams({@ApiImplicitParam(name = "file_name", value = "文件名称", paramType = "query"),
    })
    public void download(HttpServletResponse response, String file_name,String group_name) throws Exception {
        // 1. 查询数据
        List<BaseFile> list = baseFileService.query(file_name,group_name);
        // 2. 将数据填充excel导出
        // 2.1 excel头部数据
        String[] headList = new String[] { "id", "文件名称","群组名称","下载时间","扩展名称","文件大小","文件类型"};
        // 2.2 属性
        String[] fieldList = new String[] { "id", "file_name","group_name","upload_time","extName","fileSize","fileType"};
        // 2.3 数据列，需要转化
        List<Map<String, Object>> dataList = new ArrayList<>();
        for (BaseFile app : list) {
            // 每一个map相当于是一行数据，一行数据中多列
            Map<String, Object> map = new HashMap<>();
            map.put("id",app.getId());
            map.put("file_name", app.getFileName());
            map.put("group_name",app.getGroupName());
            map.put("upload_time",app.getUploadTime());
            map.put("extName",app.getExtName());
            map.put("fileSize",app.getFileSize());
            map.put("fileType",app.getFileType());
            // dataList表示所有行
            dataList.add(map);
        }
        // 2.4 导出文件名
        String excelName = "BaseFiles";
        // 3. 调用工具方法导出Excel
        ExcelUtils.createExcel(response, excelName, headList, fieldList, dataList);

    }


    @GetMapping("query")
    public Message query(String file_name,String group_name){
        List<BaseFile> list = baseFileService.query(file_name,group_name);
        return MessageUtil.success(list);
    }


    @PostMapping("saveOrUpdate")
    public Message saveOrUpdate(BaseFile baseFile){
        baseFileService.saveOrUpdate(baseFile);
        return MessageUtil.success("操作成功");
    }

    @DeleteMapping("deleteById")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户编号", required = true, paramType = "query"),
})


    public Message deleteById(@NotNull String id){
    baseFileService.deleteById(id);
    return MessageUtil.success("操作成功");
    }
}
